/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.business.domain;

import han.bifang.business.dao.DomainDao;
import han.bifang.business.dao.ServiceBusDao;
import han.bifang.business.dao.ServiceCatalogDao;
import han.bifang.business.dao.TreeDao;
import han.bifang.common.entity.SoaDomain;
import han.bifang.common.entity.SoaDomainDetail;
import han.bifang.common.entity.SoaTreeUnit;
import han.bifang.common.exception.BifangErrorCode;
import han.bifang.common.exception.BifangException;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class DomainManager {
	private static Log logger = LogFactory.getLog(DomainManager.class);

	public static ArrayList<String> createDomain(SoaTreeUnit t, SoaDomain d)
			throws BifangException {

		try {

			if (DomainDao.getDomainByName(d.getName()).size() > 0) {

				throw new BifangException(
						BifangErrorCode.DOMAIN_NAME_EXISTS_ERROR,
						"the same domain name '" + d.getName() + "' has exists");
			}
			ArrayList<String> ret = new ArrayList<String>();
			;
			TreeDao.createTreeUnit(t);
			d.setUnitId(t.getId());
			DomainDao.createDomain(d);
			t.setKey(d.getId());
			TreeDao.modifyTreeUnit(t);
			ret.add(t.getId());
			ret.add(d.getId());

			return ret;
		} catch (BifangException e) {

			throw e;

		}

	}

	public static void modifyDomain(SoaTreeUnit t, SoaDomain d)
			throws BifangException {

		try {

			TreeDao.modifyTreeUnit(t);
			DomainDao.modifyDomain(d);

		} catch (BifangException e) {

			throw e;

		}

	}

	public static void modifyDomain(SoaDomain d) throws BifangException {

		try {

			DomainDao.modifyDomain(d);
			SoaTreeUnit t = TreeDao.getTreeUnit(d.getUnitId());
			t.setName(d.getName());
			TreeDao.modifyTreeUnit(t);

		} catch (BifangException e) {

			throw e;

		}

	}

	public static void removeDomain(String treeId, String domainId)
			throws BifangException {

		try {

			List c = TreeDao.getChildren(treeId);
			if (c.size() > 0) {

				throw new BifangException(
						BifangErrorCode.TREE_HAS_CHINDREN_ERROR,
						"has children tree node");

			}
			TreeDao.removeTreeUnit(treeId);
			DomainDao.removeDomain(domainId);

		} catch (BifangException e) {

			throw e;

		}

	}

	public static void removeDomain(String domainId) throws BifangException {

		try {

			SoaDomain sd = DomainDao.getDomain(domainId);
			if (sd == null)
				throw new BifangException(
						BifangErrorCode.RECORD_NOT_EXISTS_ERROR, "not exists");
			List c = TreeDao.getChildren(sd.getUnitId());
			if (c.size() > 0) {
				throw new BifangException(
						BifangErrorCode.TREE_HAS_CHINDREN_ERROR,
						"has children tree node");

			}
			TreeDao.removeTreeUnit(sd.getUnitId());
			DomainDao.removeDomain(domainId);

		} catch (BifangException e) {

			throw e;

		}

	}

	public static List getAllDomains() throws BifangException {

		try {

			return DomainDao.getAllDomains();

		} catch (BifangException e) {

			throw e;

		}

	}

	public static SoaDomainDetail getDomainDetail(String id)
			throws BifangException {

		try {

			SoaDomainDetail dd = new SoaDomainDetail();
			dd.setDomain(DomainDao.getDomain(id));
			dd.setCatalog(ServiceCatalogDao.getServiceCatalogByDomainId(id));
			dd.setBuses(ServiceBusDao.getServiceBusByDomainId(id));

			return dd;

		} catch (BifangException e) {

			throw e;

		}

	}

	public static boolean isLocal(String id) throws BifangException {

		try {

			SoaDomain d = DomainDao.getLocalDomain();
			if(d.getId().equals(id))
				return true;
			return false;

		} catch (BifangException e) {

			throw e;

		}

	}

}
